﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IGGCore
{
    public class GameRandomizer
    {
        private Random rnd = new Random();

        public Game CreateRandomGame()
        {
            Game game = new Game();
            game.HomeTeam = new Team();
            game.HomeTeam.Code = TeamCruncher.GetRandomCode();
            TeamCruncher.CrunchTeam(game.HomeTeam);
            //game.HomeTeam.
            //game.HomeTeam.Players.Add("", GetRandomPlayer());

            game.AwayTeam = new Team();
            game.AwayTeam.Code = TeamCruncher.GetRandomCode();

            while (game.AwayTeam.Code == game.HomeTeam.Code)
            {
                game.AwayTeam.Code = TeamCruncher.GetRandomCode();
            }
            TeamCruncher.CrunchTeam(game.AwayTeam);

            game.Stadium = new Stadium();
            game.Stadium.Name = GetStadiumName(game.HomeTeam.Code);
            game.Stadium.VenueCode = "UNK";


            game.Status = new GameStatus();
            game.Status.Indicator = new Indicator("I", "I", "", Indicator.IndicatorCategoy.IN_PROGRESS);
            game.Status.Inning = 3;
            game.Status.InningTop = true;

            game.Status.CurrentBatter = GetRandomPlayer();

            for (int i = 0; i < 9; i++)
            {
                game.AwayTeam.Defense[i] = GetRandomPlayer();
                game.HomeTeam.Defense[i] = GetRandomPlayer();
            }

            return game;
        }

        private Player GetRandomPlayer()
        {
            Player player = new Player();
            player.FirstName = GetFirstName();
            player.LastName = GetLastName();
            player.Id = rnd.Next(10000000).ToString();
            return player;
        }

        private string GetFirstName()
        {
            string[] elements = { "Paul", "Ryan", "Vernon", "Brett",
                                  "Fred", "Ken", "Jeff", "Erik", "Hugh",
                                  "Pete", "Peter", "David", "Rick", "Mark",
                                  "Lou", "Tyler", "Will", "Jonah", "Albert",
                                  "Chris", "Simon", "Gregory", "Billy", "Dave",
                                  "Joe", "Troy", "Ralph"};
            return elements[rnd.Next(elements.Length)];
        }

        private string GetLastName()
        {
            string[] elements = { "Smith", "Morrison", "Douglas", "Anderson", "Grogan", "Broothe",
                                  "Thurman", "Bollinger", "Carter", "Sullivan", "Scheffer", "Wang",
                                  "Brand", "Durham", "Kochenko", "Morris", "Branyan", "King", "Holland",
                                  "O'Brien", "Simard", "Kepner", "Clark", "Harmon", "Friedrich",
                                  "Howard", "Rollins", "McNeil", "McHallan", "Randall", "Burke", "Adams",
                                  "McPherson", "Strauss", "Peters", "Abraham", "Buffo", "Barrett", "Cunningham"};
            return elements[rnd.Next(elements.Length)];
        }

        private string GetStadiumName(string code)
        {
            string stadiumName = "";
            switch (code.ToUpper())
            {
                case "LAA":
                    stadiumName = "Angel Stadium of Anaheim";
                    break;
                case "ATL":
                    stadiumName = "Turner Field";
                    break;
                case "ARI":
                    stadiumName = "Chase Field";
                    break;
                case "BAL":
                    stadiumName = "Camden Yards";
                    break;
                case "BOS":
                    stadiumName = "Fenway Park";
                    break;
                case "CHC":
                    stadiumName = "Wrigley Field";
                    break;
                case "CHW":
                    stadiumName = "U.S. Cellular Field";
                    break;
                case "CIN":
                    stadiumName = "Great American Ball Park";
                    break;
                case "CLE":
                    stadiumName = "Progressive Field";
                    break;
                case "COL":
                    stadiumName = "Coors Field";
                    break;
                case "DET":
                    stadiumName = "Comerica Park";
                    break;
                case "HOU":
                    stadiumName = "Minute Maid Park";
                    break;
                case "KC":
                    stadiumName = "Kauffman Stadium";
                    break;
                case "LAD":
                    stadiumName = "Dodger Stadium";
                    break;
                case "MIL":
                    stadiumName = "Miller Park";
                    break;
                case "MIA":
                    stadiumName = "Marlins Park";
                    break;
                case "MIN":
                    stadiumName = "Target Field";
                    break;
                case "NYM":
                    stadiumName = "Citi Field";
                    break;
                case "NYY":
                    stadiumName = "Yankee Stadium";
                    break;
                case "OAK":
                    stadiumName = "McAfee Coliseum";
                    break;
                case "PHI":
                    stadiumName = "Citizens Bank Park";
                    break;
                case "PIT":
                    stadiumName = "PNC Park";
                    break;
                case "SD":
                    stadiumName = "Petco Park";
                    break;
                case "SF":
                    stadiumName = "AT&T Park";
                    break;
                case "SEA":
                    stadiumName = "Safeco Field";
                    break;
                case "STL":
                    stadiumName = "Busch Stadium";
                    break;
                case "TB":
                    stadiumName = "Tropicana Field";
                    break;
                case "TEX":
                    stadiumName = "Rangers Ballpark In Arlington";
                    break;
                case "TOR":
                    stadiumName = "Rogers Centre";
                    break;
                case "WAS":
                    stadiumName = "Nationals Park";
                    break;
            }
            return stadiumName;
        }
    }
}
